package com.learn;

import java.util.Arrays;

public class InsertSort {

    public static void main(String[] args) {
        int[] arrs = new int[]{4, 2, 6, 1, 8, 9, 5};
        insertSort(arrs);
        System.out.println(Arrays.toString(arrs));
    }

    public static void insertSort(int[] arrs) {
        int length = arrs.length;
        if (arrs == null || length < 2) {
            return;
        }

        for (int i = 1; i < length; i++) {
            int j = i;
            while (j - 1 >= 0 && arrs[j] < arrs[j - 1]) {
                int swap = arrs[j];
                arrs[j] = arrs[j - 1];
                arrs[j - 1] = swap;
                j--;
            }
        }
    }
}
